package com.marquisx.common.database;

import java.util.List;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public abstract class DBAdapter
{	
	protected static final String TAG = "MarquisX:DBAdapter";
	
	protected static final String DATABASE_CREATE = "";
	protected static final String DATABASE_NAME = "";
	protected static final String DATABASE_TABLE = "";
	protected static final int DATABASE_VERSION = 1;
	
	protected static final String KEY_ID = "_id";
	protected static final int KEY_COLUMN = 0;
	
	// Variable to hold the database instance
	private SQLiteDatabase db;
	// Context of the application using the database.
	private final Context context;
	// Database open/upgrade helper
	private DBHelper dbHelper;
	
	public DBAdapter(Context context)
	{
		this.context = context;
		dbHelper = new DBHelper(this.context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	public DBAdapter open() throws SQLException
	{
		try
		{
			db = dbHelper.getWritableDatabase();
		}
		catch (SQLiteException ex)
		{
			Log.e(TAG, ex.getMessage());
			db = dbHelper.getReadableDatabase();
		}
		return this;
	}
	
	public void close()
	{
		if( db != null )
		{
			db.close();
		}
	}
	
	abstract public long insertEntry(Object obj);
	abstract public boolean updateEntry(Object obj);
	abstract public boolean removeEntry(Object obj);
	abstract public Object getEntry(long rowIndex);
	abstract public List<Object> getAllEntries();
	
	public boolean removeEntry(long rowIndex)
	{
		return db.delete(DATABASE_TABLE, KEY_ID  + "=" + rowIndex, null) > 0;
	}
	
	public boolean deleteAll()
	{
		return db.delete(DATABASE_TABLE, null, null) > 0;
	}
	
	public void insertEntries(List<Object> list)
	{
		assert (list != null && list.size() !=0);
		
		for( Object obj : list)
		{
			insertEntry(obj);
		}
		
	}
	
	private static class DBHelper extends SQLiteOpenHelper
	{
		private static final String TAG = "MarquisX: DBHelper";
		public DBHelper(Context context, String name, CursorFactory factory, int version)
		{
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db)
		{
			Log.i(TAG, "create db using SQL: " + DATABASE_CREATE);
			db.execSQL(DATABASE_CREATE);
		}

		// Called when there is a database version mismatch meaning that the version 
		// of the database on disk needs to be upgraded to the current version.
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			// Log the version upgrade
			Log.w(TAG, "Upgrading from version " + 
						oldVersion + " to " + 
						newVersion + ", which will destroy all old data");
			
			// Upgrade the existing database to conform to the new version. Multiple
			// previous versions can be handled by comparing oldVersion and newVersion
			// values.
			
			// The simplest case is to drop the old table and create a new one.
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			// Create a new one.
			onCreate(db);
		}
	}
}
